﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
    public class ThietBiThongDungDAO
    {
        public static int KiemTraThietBi(string tbtd)
        {

            SqlConnection conn = DataProvider.Connection();
            conn.Open();
            string sql = "usp_KiemTraTBTD";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@TenTb", SqlDbType.NVarChar).Value = tbtd;
            cmd.Parameters.Add("@k", SqlDbType.Int);
            cmd.Parameters["@k"].Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            int k = Int32.Parse(cmd.Parameters["@k"].Value.ToString());
            if (k == 1)
                return 1;
            else
                return 0;
        }


        public static DataTable DanhSachTBTD()
        {
            SqlConnection cn = DataProvider.Connection();
            string sql = "usp_DanhSachTBTD";
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cn.Close();
            return dt;
        }

        public static DataTable LayDSTBTDTheoMa(int ma)
        {
            SqlConnection cn = DataProvider.Connection();
            string sql = "usp_ThongTinTBTD";
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add("@Ma", SqlDbType.Int).Value = ma;
            DataTable dt = new DataTable();
            da.Fill(dt);
            cn.Close();
            return dt;
        }

        public static int ThemTBTD(ThietBiThongDungDTO tbtd)
        {
            SqlConnection conn = DataProvider.Connection();
            conn.Open();
            string sql = "usp_ThemTBTD";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@TenTb", SqlDbType.NVarChar).Value = tbtd.TenTBTD;
            cmd.Parameters.Add("@CongSuatTT", SqlDbType.Float).Value = tbtd.CongSuatTieuThu;
            cmd.Parameters.Add("@CongSuatTH", SqlDbType.Float).Value = tbtd.CongSuatTieuHao;
            cmd.Parameters.Add("@HeSo", SqlDbType.Int).Value = tbtd.HeSoDeBa;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq >= 0)
                return 1;
            else
                return 0;
        }

        public static int CapNhatTBTD(ThietBiThongDungDTO tbtd)
        {
            SqlConnection conn = DataProvider.Connection();
            conn.Open();
            string str = "usp_CapNhatTBTD";
            SqlCommand cmd = new SqlCommand(str, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MaTB", SqlDbType.Int).Value = tbtd.MaThietBiThongDung;
            cmd.Parameters.Add("@TenTb", SqlDbType.NVarChar).Value = tbtd.TenTBTD;
            cmd.Parameters.Add("@CongSuatTT", SqlDbType.Float).Value = tbtd.CongSuatTieuThu;
            cmd.Parameters.Add("@CongSuatTH", SqlDbType.Float).Value = tbtd.CongSuatTieuHao;
            cmd.Parameters.Add("@HeSo", SqlDbType.Int).Value = tbtd.HeSoDeBa;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq >= 0)
                return 1;
            else
                return 0;
        }
    }
}
